﻿using A.M.ETO;
using A.M.Storage.Application;
using L.M.System.Application.IServices;
using L.M.System.Domain.DTO;
using L.M.System.Domain.Entity;
using L.M.System.Infrastruecure.IRepository;
using L.M.System.Infrastruecure.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace L.M.System.Application.Services
{
    public class Authority_list_inforServices : BaseServices<authority_list_infor, string>, IAuthority_list_inforServices
    {
        private readonly IAuthority_list_inforRepository authority_List_InforRepository;
        /// <summary>
        /// 用户角色
        /// </summary>
        private readonly IEmployee_role_tableServices employee_Role_TableServices;
        /// <summary>
        /// 角色子权限
        /// </summary>
        private readonly IRole_authorit_listServices role_Authorit_ListServices;

        public Authority_list_inforServices(IAuthority_list_inforRepository authority_List_InforRepository, IEmployee_role_tableServices employee_Role_TableServices, IRole_authorit_listServices role_Authorit_ListServices) : base(authority_List_InforRepository)
        {
            this.authority_List_InforRepository = authority_List_InforRepository;
            this.employee_Role_TableServices = employee_Role_TableServices;
            this.role_Authorit_ListServices = role_Authorit_ListServices;
        }

        /// <summary>
        /// 递归查询左侧导航信息
        /// </summary>
        /// <param name="mId">编号</param>
        /// <param name="uId">用户编号</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<MinInfor>> GetShowAuthority_list_infor(string? mId,string? uId)
        {
            ApiReturn<MinInfor> apiReturn = new ApiReturn<MinInfor>();
            List<employee_role_table>? userRole = await employee_Role_TableServices.GetShowInforAsync();
            List<role_authorit_list>? roleLinTwo = await role_Authorit_ListServices.GetShowInforAsync();
            List<authority_list_infor>? liInforTwo = await authority_List_InforRepository.GetShowInforAsync();
            List<authority_list_infor>? list = (from a in userRole
                        join b in roleLinTwo
                        on a.role_number equals b.role_number
                        join c in liInforTwo
                        on b.aid equals c.aid
                        where (a.employee_number == uId)
                        select c).ToList();
            list = list.OrderBy(c => c.orderid).ToList();
           var mList =  GetMinfor(list, mId);
            apiReturn.Code = 200;
            apiReturn.Message = "成功";
            apiReturn.ListInfor = mList;
            return apiReturn;
        }

        public List<MinInfor> GetMinfor(List<authority_list_infor> list ,string? mId)
        {
            var mlist = list.Where(c => c.fid == mId).ToList().Select(c => new MinInfor
            {
                label = c.aid,
                value = c.aname,
                aurl = c.aurl,
                uview = c.aview,
                children = GetMinfor(list, c.aid)
            }).ToList();
            return mlist.Count() == 0? null : mlist;
        }

         
    }
}
